<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <div class="search-list-content">
      <van-list
      v-model="loading"
      :finished="done"
      :immediate-check="false"
      offset="50"
      @load="pullUploadMoreFn"
      >

        <ArticleItem
        v-for="item in resultList"
        :key="item.art_id"
        :item="item"
        :isShowCross="false"
        />

      </van-list>
    </div>
  </div>
</template>

<script>
import { getSearchResultListAPI } from '@/api/search'
let page = 1
export default {
  name: 'SearchResult',
  data () {
    return {
      resultList: [],
      loading: false,
      done: false

    }
  },
  async created () {
    this.getSearchResultList()
  },
  computed: {
    txt () {
      return this.$route.params.keyword
    }
  },
  methods: {
    // 封装起来，直接调用
    async  getSearchResultList () {
      const resp = await getSearchResultListAPI({
        keyword: this.txt,
        page: page++
      })
      // 解构一下
      const { results } = resp.data
      if (results.length < 10) {
        this.done = true
        return
      }
      this.resultList = [...this.resultList, ...results]
      this.loading = false
    },
    // 上拉加载更多，
    pullUploadMoreFn () {
      if (this.resultList.length > 0) {
        this.getSearchResultList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
@nav-bar-icon-color: white;
</style>
